package com.cb.common.menu;

import java.io.Serializable;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.cb.domain.model.system.AppRole;
import com.cb.domain.model.system.SystemUser;
import com.cb.domain.model.user.UserAccount;

public class Menu implements Serializable{
	
	final static String ROLE_SEPARATE = ",";

	private static final long serialVersionUID = 1L;
	
	private String title="";
	private String name="";
	private String page="";
	private String roles="";
	private String description;
	private String pattern="";
	
	private List<Menu> items;
	
	public boolean isVisible(SystemUser currentUser) {
		if (StringUtils.isBlank(roles))
			return false;
		for (AppRole role : currentUser.getRoles()) {
			if (StringUtils.contains(roles, role.getRoleName()))
				return true;
		}
		return false;
	}

	public void addPermitRole(String roleName) {
		if (StringUtils.contains(roles, roleName))
			return;
		
		if (StringUtils.isNotBlank(roles)) {
			roles += ROLE_SEPARATE + roleName; 
		} else {
			roles = roleName;
		}
	}

	public void removePermitRole(String roleName) {
		if (!StringUtils.contains(roles, roleName))
			return;
		
		if (roles.length() > roleName.length()) {
			if (StringUtils.contains(roles, ROLE_SEPARATE+roleName)) {
				roles = StringUtils.remove(roles, ROLE_SEPARATE+roleName);
			} else {
				roles = StringUtils.remove(roles, roleName+ROLE_SEPARATE);
			}
		} else {
			roles = "";
		}
	}
	
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPage() {
		return page;
	}
	public void setPage(String page) {
		this.page = page;
	}
	public String getRoles() {
		return roles;
	}
	public void setRoles(String roles) {
		this.roles = roles;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public List<Menu> getItems() {
		return items;
	}
	public void setItems(List<Menu> items) {
		this.items = items;
	}
	public String getPattern() {
		return pattern;
	}
	public void setPattern(String pattern) {
		this.pattern = pattern;
	}
	
}
